import $ from 'zepto-webpack'
import 'src/assets/js/rem'
import './index.less'
import {apiConfig, api, version} from "../../config";
import {decrypt, getRequestParams} from "../../utils/requestParams";
import formItemArt from './formItem.art'

$(function () {

});


getLargeLoanConfig();

function getLargeLoanConfig() {
  var userInfo = window.sessionStorage.getItem(apiConfig.mmh_user_info) ? JSON.parse(window.sessionStorage.getItem(apiConfig.mmh_user_info)) : {};
  if (!userInfo.uid) {
    return false
  }
  $.ajax({
    type: 'POST',
    url: apiConfig.domain + '/' + version.api_version + api.large_loan_config,
    data: getRequestParams({
      uid: userInfo.uid,
    }),
    success: function (response) {
      var res = JSON.parse(response);
      var listData = JSON.parse(decrypt(res.data));
      if (res.code === 200) {
        var html = formItemArt(listData);
        $('#form-item-content').html(html);
        $('.save-button').show();
        WatchRadio();
      } else {
        toast(res.msg)
      }
    },
    error: function (error) {

    }
  })
}

$('#submitForm').submit(function (event) {
  event.preventDefault();
  var fieldInfo = serializeArray();
  var length = fieldInfo.length;
  for (var i = 0; i < length; i++) {
    var fieldItem = fieldInfo[i];
    if (!fieldItem.value) {
      if (fieldItem.name === "user_name" || fieldItem.name === "id_card" || fieldItem.name === "income") {
        toast('请填写' + fieldItem.title);
      } else {
        toast('请选择' + fieldItem.title);
      }
      return false;
      break;
    }
  }

  var formData = {};
  for (var i = 0; i < length; i++) {
    formData[fieldInfo[i].name] = fieldInfo[i].value;
  }

  // 开始提交数据

  FormSubmit(formData)

});

//提交表单信息
function FormSubmit(data) {
  $('.save-button').attr('disabled', 'disabled');
  var userInfo = window.sessionStorage.getItem(apiConfig.mmh_user_info) ? JSON.parse(window.sessionStorage.getItem(apiConfig.mmh_user_info)) : {};
  // if (!userInfo.uid) {
  //   return false
  // }
  data.uid = userInfo.uid;
  data.city = '武汉';

  // 根据接口判断 用户提交状态
  $.ajax({
    type: 'POST',
    url: apiConfig.domain + '/' + version.api_version + api.large_loan_order_status,
    data: getRequestParams({uid: userInfo.uid}),
    success: function (response) {
      $('.save-button').removeAttr('disabled');
      var res = JSON.parse(response);
      var resultInfo = JSON.parse(decrypt(res.data));
      if (res.code === 200) {
        // console.log(resultInfo.order_no);
        // console.log(resultInfo.status);
        if (resultInfo.status == 0 || resultInfo.status == 4) {
          FormSubmitData(data)
        } else {
          toast('已有提交订单,正在处理中')
        }
      } else {
        toast(res.msg)
      }
    },
    error: function (error) {
      $('.save-button').removeAttr('disabled');
    }
  });
}

//

function FormSubmitData(data) {
  $.ajax({
    type: 'POST',
    url: apiConfig.domain + '/' + version.api_version + api.apply_loan,
    data: getRequestParams(data),
    success: function (response) {
      $('.save-button').removeAttr('disabled');
      var res = JSON.parse(response);
      if (res.code === 200) {
        window.location.href = './success.html'
      } else {
        toast(res.msg)
      }
    },
    error: function (error) {
      $('.save-button').removeAttr('disabled');
    }
  })
}


function serializeArray() {
  var allFields = [];
  $('#submitForm .submit-filed').each(function (index, ele) {
    allFields.push({
      name: ele.name,
      value: ele.value,
      title: ele.title
    });
  });
  return allFields
}

//监听radio
window.WatchRadio = () => {
  $('input[type="radio"]').on('change', function () {
    $(this).parent().parent().siblings('input').val($(this).val());
  })
}


function toast(text) {
  $('.toast-box').addClass('show').html(text);
  setTimeout(function () {
    $('.toast-box').removeClass('show').html('');
  }, 2500)
}



